var $ = function(selector){return document.querySelector(selector)}
var $$ = function(selector){return document.querySelectorAll(selector)}

var container = $(".container")
var wrapper = $(".wrapper")
// 开始按钮
var startbtn = $("#startbtn")
// 重新开始按钮
var restartbtn = $("#restartbtn")
// 子弹数组
var bullets = []
// 敌机数组
var enemies = []
// 子弹飞行速度
var BULLET_SPEED = 10;
// 敌机的飞行速度
var ENEMY_SPEED = 3;
// 屏幕宽高
var winWidth = document.body.offsetWidth;
var winHeight = document.body.offsetHeight

var scoreLabel = $(".score")
var score = 0;

// 游戏结束页
var gameOverPage = $(".game-over")
var gameOverScore = $("#game-over-score")

var hero,bulletTimer,enemyTimer,updateTimer;
var bgoffset = 0;

var bgMusic = $("#bgmusic")
var bombMusic = $("#bomb")

// window.addEventListener("keyup",function(e){
//     console.log(e)
//     if(e.code === "Space"){
//         var bullet = new Plane(14,31,"./img/bullet1.png",hero.x + hero.width/2 - 7,hero.y-31)
//         bullets.push(bullet) 
//     }
// })



var enemyUrls = [
    "./img/enemy1.png",
    "./img/enemy2.png",
    "./img/enemy3.png",
    "./img/enemy4.png",
]


function heroBorn(){
    // 生成小飞机
    hero = new Plane(109,82,"./img/hero.png",150,500)
}

// 自动发射子弹
function fire(){
    bulletTimer = setInterval(function(){
    // 生成一个子弹
    var bullet = new Plane(14,31,"./img/bullet1.png",hero.x + hero.width/2 - 7,hero.y-31)
    bullets.push(bullet)
    },200)
}

//生成敌机
function makeEnemy(){
    // 敌机
    enemyTimer = setInterval(function(){
        var urlIndex = Math.floor(Math.random() * enemyUrls.length)
        var x = Math.random()*(winWidth - 120)
        var ene = new Plane(120,100,enemyUrls[urlIndex],x,0)
        enemies.push(ene)
    },300)
}

//
function updateTime(){
    updateTimer = setInterval(function(){

    var isBreak = false;
    for(var i = 0 ;i < bullets.length;i++){

        var b = bullets[i]
        b.fly(null,b.y - BULLET_SPEED)
        if(b.y < -20){
            b.die()
            bullets.splice(i,1)
        }
        // 检测子弹是否打中敌机
        for(var j = 0;j < enemies.length;j++){
            var e = enemies[j]
            if(collision(b,e) && e.y >= 100){
                setScore()
                enemies.splice(j,1)
                playBombMusic()
                e.die()                            
                break;
            }
        }
    }

    // 敌机飞行
    for(var i  = 0;i < enemies.length;i++){
        var e = enemies[i]
        e.fly(null,e.y + ENEMY_SPEED)

        if(e.y > winHeight){
            e.die()
            enemies.splice(i,1)
            playBombMusic()
        }

        // 检测hero和敌机是否发生碰撞
        if(collision(hero,e)){

            e.die()
            enemies.splice(i,1)
            hero.die()
            gameOver()
            break;
        }
    }

    // 移动地图
    container.style.backgroundPositionY = bgoffset+"px"
    bgoffset++;
},20)
}


// 设置分数
function setScore(){
    score++;
    scoreLabel.innerHTML = score
}


// 游戏结束
function gameOver(){
    clearInterval(bulletTimer)
    clearInterval(enemyTimer)
    clearInterval(updateTimer)

    // 显示结束页
    gameOverPage.classList.remove("hide")
    // 设置分数
    gameOverScore.innerHTML = score
}


// 碰撞检测
function collision(a,b){
    // if(a.x + b.width >= a.x 
    //     && b.x <= a.x + a.width 
    //     && b.y + b.height >= a.y 
    //     && b.y>= a.y + a.height){
    //         return true
    // }else{
    //     false
    // }

    var acx = a.x + a.width/2;
    var acy = a.y + a.height/2;

    var bcx = b.x + b.width/2;
    var bcy = b.y + b.height/2;


    // a*a + b*b = c * c   
    // Math.sqart

    if((acx-bcx)*(acx-bcx) + (acy-bcy)*(acy-bcy) < (b.height)*(b.height)){
        return true
    }else{
        false
    }
}

function startGame(){
    // 生成hero
    heroBorn()
    fire()
    makeEnemy()
    updateTime()
    playBgMusic()
}

// 播放背景音乐
function playBgMusic(){
    bgMusic.play()
}
function playBombMusic(){
    // 把播放进度拉到开头
    bombMusic.currentTime = 0;
    bombMusic.play()
}


restartbtn.onclick = function(){
    // 隐藏技术界面
    gameOverPage.classList.add("hide")
    // 分数清零
    score = 0;
    setScore()
    // 数组清空
    bullets = []
    enemies = []
    // 清空界面
    wrapper.innerHTML = ""
    
    startGame()
}

// 点击开始按钮
startbtn.onclick = function(){
    $(".start-page").classList.add("hide")
    $(".container").classList.remove("hide")
    startGame()
}


// 控制飞机移动
container.addEventListener("touchmove",function(e){

    var touch = e.touches[0]
    var x = touch.clientX;
    var y = touch.clientY;

    hero.fly(x,y)
})